package com.audaque.springboot.foshanupload.web.amqpdemo.listener;

import com.audaque.springboot.foshanupload.core.util.StringUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues="directQueueTx")
public class AckListener {
    @RabbitHandler
    public void process(String msg, Channel channel, Message message) throws IOException {
        try {
            if(StringUtils.isNotBlank(msg)){
                String numstr = msg.substring(3);
                Integer num = Integer.parseInt(numstr);
                if(num >= 3){
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                            false, true);
                    System.out.println("get msg2 basicNack msg = "+msg);
                }else{
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    System.out.println("get msg2 basicAck msg = "+msg);
                }
            }
        } catch (Exception e) {
            //消费者处理出了问题，需要告诉队列信息消费失败
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                    false, true);
            System.err.println("get msg2 failed msg = "+msg);
        }
    }


}